<template>
	<view style="padding: 5px">
		<uni-row>
			<uni-col>
				<uni-datetime-picker v-model="rq" type="date" :clear-icon="false" :end="Date.now()" @change="getBadDorHygieneByClassId"/>
			</uni-col>
		</uni-row>
		
		<uni-card :title="item.dormitoryNo" :extra="item.buildingNo" padding="0" spacing="0" v-for="(item,index) in hygieneList" :key="index">
			<uni-list>
				<uni-list-item v-for="(hygiene,no) in item.info" :key="no" :border="false" :showArrow="hygiene.checkTimeSpan!==null" 
					@click="toDetailsPage(item,hygiene)" :clickable="hygiene.checkTimeSpan!==null">
					<template v-slot:body>
						<view style="margin-bottom: 2px;" v-if="hygiene.checkTimeSpan!=null">{{getTimeSpan(hygiene.checkTimeSpan)}}</view>
						<view :class="{'bad':hygiene.grade === 3,'pass':hygiene.grade === 2,'good':hygiene.grade === 1 }">检查结果：{{getGrade(hygiene.grade)}}</view>
					</template>
				</uni-list-item>
			</uni-list>
		</uni-card>
	</view>
</template>

<script>
	export default {
		onLoad(options) {
			this.rq = options.rq;
			this.classId = options.classId;
			this.className = options.className;
			this.getBadDorHygieneByClassId();
		},
		data() {
			return {
				hygieneList:[],
				rq: '',
				classId : '',
				className : ''
			}
		},
		methods: {
			toDetailsPage(item,hygiene) {
				if(hygiene.checkTimeSpan!==null) {
					uni.navigateTo({
						url : `/pages_duty/classTeacher/dormitoryHygiene/HygieneDetails?dormitoryId=${item.dormitoryId}&dormitoryNo=${item.dormitoryNo}&checkTimeSpan=${hygiene.checkTimeSpan}&rq=${this.rq}`
					})
				}
			},
			getBadDorHygieneByClassId() {
				this.$api.dormitory.getBadDorHygieneByClassId(this.classId,this.rq).then( hygieneRes => {
					console.log("宿舍卫生信息：",hygieneRes.data);
					
					const map = new Map();  
					hygieneRes.data.forEach(item => {  
						if (!map.has(item.dormitoryId)) {  
						  map.set(item.dormitoryId, {  
							dormitoryId:item.dormitoryId,
							buildingNo:item.buildingNo,
							dormitoryNo:item.dormitoryNo,
							info:[{
								id:item.id,
								checkTimeSpan:item.checkTimeSpan,
								grade:item.grade
							}]
						  });  
						} else {  
						  const existingItem = map.get(item.dormitoryId);  
						  existingItem.info.push({
							  id:item.id,
							  checkTimeSpan:item.checkTimeSpan,
							  grade:item.grade
						  })  
						}  
					});  

					this.hygieneList = [...map.values()];  
				})
			},
			getTimeSpan(timeSpan) {
				if(timeSpan === 0) {
					return '月假';
				}else if(timeSpan === 1) {
					return '上午';
				}else {
					return '下午';
				}
			},
			getGrade(grade) {
				if(grade === 1) {
					return '优秀';
				}else if(grade === 2) {
					return '良';
				}else if(grade === 3){
					return '差';
				} else {
					return '未检查'
				}
			}
		}
	}
</script>


<style>
	.bad {
		color: red;
	}
	.good {
		color : green;
	}
	.pass {
		color : dodgerblue;
	}
</style>
